package yu.zhou.njupt.fileprocessor;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import yu.zhou.njupt.fileprocessor.bean.CaseSensitive;
import yu.zhou.njupt.fileprocessor.bean.MatchMode;

/**
 * @author yu.zhou
 *
 */
public class TestCase {
    
    public final static Logger logger = LoggerFactory.getLogger(Main.class);
    public final static Properties CONFIGURE = new Properties();

    @Test
    public void testFileCopy() {
        Map<String, String> filenameMap = new HashMap<String, String>();
        
        filenameMap.put("123.txt", "qqq.txt");
        filenameMap.put("456.txt", "ttt.txt");

        try {
            int count = new FileCopy(1).work("e:/test", "e:/test_bak", filenameMap, MatchMode.EQUALS, CaseSensitive.FALSE);
            System.out.println(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @Test
    public void testFileDelete() {
        List<String> filenameList = new ArrayList<String>();

        filenameList.add("456.txt");
        filenameList.add("789.txt");

        try {
            int count = new FileDelete(1).work("e:/test", "e:/test_bak", filenameList, MatchMode.EQUALS, CaseSensitive.FALSE);
            System.out.println(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @Test
    public void testFileRename() {
        Map<String, String> filenameMap = new HashMap<String, String>();
        filenameMap.put("123.txt", "456.txt");
        filenameMap.put("128911.txt", "456.txt");

        try {
            int count = new FileRename(1).work("e:/test", "e:/test_bak", filenameMap, MatchMode.EQUALS, CaseSensitive.FALSE);
            System.out.println(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @Test
    public void main() {
        try {
            List<String> deleteFilenameList = new ArrayList<String>();
            List<String> deleteFilenameList2 = new ArrayList<String>();
            Map<String, String> renameFilenameMap = new HashMap<String, String>();
            
            deleteFilenameList.add("123.swf");
            deleteFilenameList.add("456.swf");
            
            deleteFilenameList2.add("123-1.swf");
            deleteFilenameList2.add("456-1.swf");
            
            renameFilenameMap.put("123-1.swf", "123.swf");
            renameFilenameMap.put("456-1.swf", "456.swf");
            
            // 删除旧图片
            String sourthPathStr = "E:/test";
            String renamePathStr = "E:/test_rename";
            String bakPathStr = "E:/test_bak";
            logger.info("开始删除" + sourthPathStr + "目录下的旧图片,请稍候...");
            int numOfDeletedFiles = new FileDelete().work(sourthPathStr, bakPathStr, deleteFilenameList, MatchMode.EQUALS, CaseSensitive.FALSE);
            logger.info("旧图片删除完毕.删除的文件数:" + numOfDeletedFiles); 
            logger.info("被删除的图片备份在" + bakPathStr);
            
            // 重命名图片
            logger.info("开始重命名" + sourthPathStr + "目录下的名称包含'-'的图片,请稍候...");
            int numOfRenamedFiles = new FileCopy().work(sourthPathStr, renamePathStr, renameFilenameMap, MatchMode.EQUALS, CaseSensitive.FALSE);
            new FileDelete().work(sourthPathStr, bakPathStr, deleteFilenameList2, MatchMode.EQUALS, CaseSensitive.FALSE);
            logger.info("图片重命名完毕.重命名的文件数:" + numOfRenamedFiles);
            logger.info("重命名后的图片在:" + renamePathStr);
            logger.info("被重命名的图片备份在" + bakPathStr);
        } catch (IOException e) {
            logger.error("未找到配置文件!!!", e);
        } catch (ClassNotFoundException e) {
            logger.error("数据库驱动类名称步正确!!!", e);
        } catch (SQLException e) {
            logger.error("连接数据源失败!!!", e);
        } catch (Exception e) {
            logger.error("处理文件过程中出现异常!!!", e);
        } finally {
        }
    }
    
}